package com.lv.config;

import com.lv.filter.CaptchaFilter;
import com.lv.filter.TokenAuthFilter;
import com.lv.handler.*;
import com.lv.service.impl.UserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * Created with IntelliJ IDEA.
 * <p>
 * security配置
 * </p>
 *
 * @Author: 凉薄
 * @Date: 2022/01/07/9:21
 * @Description:
 */

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    MyFailureHandler myFailureHandler;
    @Autowired
    MySuccessHandler mySuccessHandler;
    @Autowired
    MyAccessDeniedHandler myAccessDeniedHandler;
    @Autowired
    MyAuthenticationEntryPoint myAuthenticationEntryPoint;
    @Autowired
    UserDetailServiceImpl userDetailService;
    @Autowired
    MyLogoutSuccessHandler myLogoutSuccessHandler;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.userDetailsService(userDetailService);

    }


    @Autowired
    CaptchaFilter captchaFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().cors().and()

               .authorizeRequests().antMatchers("favicon.ico","/test","/login","/getCaptcha","/druid/**","/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui/**").permitAll()
                .anyRequest().authenticated();
        //登录配置
        http.formLogin().failureHandler(myFailureHandler).successHandler(mySuccessHandler);
        //禁用session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //异常处理器
        http.exceptionHandling().authenticationEntryPoint(myAuthenticationEntryPoint)
                .accessDeniedHandler(myAccessDeniedHandler);
        //配置自定义过滤器
        http.addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class);
        http.addFilter(tokenAuthFilter());
        //退出
        http.logout().logoutSuccessHandler(myLogoutSuccessHandler);
    }


    @Override
    public void configure(WebSecurity web) throws Exception {

            web.ignoring().antMatchers("/druid/**","/swagger-ui/**","/webjars/**","/avatar/**");
            //可以仿照上面一句忽略静态资源

    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Bean
    TokenAuthFilter tokenAuthFilter() throws Exception{
        return new TokenAuthFilter(authenticationManager());
    }
}
